package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.InterfaceTokenDao;
import com.slipper.system.codeless.bo.InterfaceTokenBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class InterfaceTokenService {
    @Resource
    private InterfaceTokenDao interfaceTokenDao;
    
    public InterfaceTokenBO queryBy(String id) {
        return interfaceTokenDao.queryBy(id);
    }
    
    public List<InterfaceTokenBO> queryList(InterfaceTokenBO bo) {
        return interfaceTokenDao.queryList(bo);
    }
    
    public PageInfo<InterfaceTokenBO> queryPage(InterfaceTokenBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(interfaceTokenDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(InterfaceTokenBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return interfaceTokenDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return interfaceTokenDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public int delete(InterfaceTokenBO bo) {
        return interfaceTokenDao.delete(bo);
    }

    public InterfaceTokenBO queryToken(String appKey, String appSecret) {
        List<InterfaceTokenBO> tokenList = interfaceTokenDao.queryToken(appKey, appSecret);
        return tokenList.isEmpty() ? null : tokenList.getFirst();
    }
}
